package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.base.EditableListener;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.EnvironmentalFeesDialog;
import com.moleq.mgpos.activities.R;
import com.moleq.models.BottleSetting;

public class BottleSetupAdapter extends MQBaseAdapter implements OnResultListener {

	public static final String BOTTLE_EDITED = "BottleEdit";
	private LayoutInflater mInflater;
	private List<BottleSetting> listBottle;
	private BottleSetting mbottleSetting;
	private EditableListener mEditableListener;
	private Context shareContext;

	public BottleSetupAdapter(Context context, List<BottleSetting> listBottle) {
		this.mInflater = LayoutInflater.from(context);
		this.shareContext = context;
		this.listBottle = listBottle;
	}

	public void setEditableListener(EditableListener listener) {
		mEditableListener = listener;
	}

	public void setSource(List<BottleSetting> listBottle) {
		this.listBottle = listBottle;
	}

	public int getCount() {
		return listBottle.size();
	}

	public Object getItem(int position) {
		return listBottle.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		super.getView(position, convertView, parent);
		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ps_bottle_row, null);
			holder.tv_bottleno = (TextView) convertView.findViewById(R.id.tv_bottleno);
			holder.tv_bottledesc = (TextView) convertView.findViewById(R.id.tv_bottledesc);
			holder.tv_bottleamt = (TextView) convertView.findViewById(R.id.tv_bottleamt);
			holder.iv_set = (ImageView) convertView.findViewById(R.id.iv_set);
			holder.llBottleRow = (LinearLayout) convertView.findViewById(R.id.llBottleRow);

			holder.iv_set.setOnClickListener(onClickListener);
			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}

		holder.tv_bottleno.setText(String.valueOf(position + 1));
		listBottle.get(position).setNo(position + 1);
		
		holder.iv_set.setTag(position);
		holder.tv_bottledesc.setText(listBottle.get(position).getBo_desc());

		double amt = listBottle.get(position).getBo_amt();
		if (amt != 0) {
			holder.tv_bottleamt.setText(String.format("%.2f",amt));
		} else {
			holder.tv_bottleamt.setText("");
		}

		if (position == selectedRow) {
			holder.llBottleRow.setBackgroundColor(Color.rgb(255, 219, 16));
		}else {
			holder.llBottleRow.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}

	public OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View view) {

			switch (view.getId()) {
			case R.id.iv_set:
				selectedRow = (Integer) view.getTag();
				ScrollToRow(selectedRow);
				mbottleSetting = listBottle.get(selectedRow);

				EnvironmentalFeesDialog mEnvironmentalFeesDialog = new EnvironmentalFeesDialog(shareContext, BottleSetupAdapter.this);
				mEnvironmentalFeesDialog.setSource(listBottle, mbottleSetting, selectedRow);
				mEnvironmentalFeesDialog.show();
				break;
			}
		}
	};

	@Override
	public void onTextChange(int resId, String changedText)  {
		
//		switch (resId) {
//		case R.id.et_bottledesc:
//			listBottle.get(selectedRow).setBo_desc(changedText);
//			break;
//		case R.id.et_bottleamt:
//			if (MoleQUtil.isNumeric2(changedText)) {
//				listBottle.get(selectedRow).setBo_amt(Double.valueOf(changedText));
//			}else{
//				listBottle.get(selectedRow).setBo_amt(0);
//			}
//			break;
//		}
//		listBottle.get(selectedRow).setEdit(true);
//		mEditableListener.onEdited(BOTTLE_EDITED);
	}

	public final class ViewHolder {
		public TextView tv_bottleno;
		public TextView tv_bottledesc;
		public TextView tv_bottleamt;
		public ImageView iv_set;
//		public TextView tv_set;
		private LinearLayout llBottleRow;
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass, int viewId, String returnvalue) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		listBottle = (List<BottleSetting>) obj;
		mEditableListener.onEdited(BOTTLE_EDITED);
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub

	}
}
